﻿@using Telerik.Web.Mvc
@using Telerik.Web.Mvc.UI
@using Presentation.Web.Admin
@model  IEnumerable<Domain.Dto.PushContentsDto>
@{
    ViewBag.subtitle = "推送历史内容管理";
    ViewBag.title = "推送历史内容管理";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@{Html.Telerik().Grid(Model)
    .Name("PushContentGrid")
    .ToolBar(toolBar => toolBar.Template(
    @<text>
    @{
        if (Html.HasUiPermission(this.ViewContext, "searchpushcontent"))
        {
        <label class="customer-label" for="Customers-input">
            历史内容:
        </label>
        <input type="text" id="searchPushContent" value="历史内容" data-autocomplete="/push/searchpushcontent" />
        <input type='button' value='我要搜索' class='t-button' onclick='searchChange()' />
        }
        if (Html.HasUiPermission(this.ViewContext, "muldeletepushcontent"))
        { 
        <a href='javascript:;' class='t-button' onclick='mulDelete()'>删除选择项</a>
        }
    }
    </text>))
    .Columns(columns =>
    {
        columns.Template(@<text><input name="selectRow" type="checkbox" value="@item.Id " title="checkedRecords" /></text>)
        .HeaderTemplate(@<text><input type="checkbox" title="选择所有" id="checkAllRecords" /></text>)
        .Width(50).HeaderHtmlAttributes(new { style = "text-align:center" }).HtmlAttributes(new { style = "text-align:center" })
        .ClientTemplate("<input type=\"checkbox\" title=\"check all records\" name = 'selectRow' value = '<#= Id #>'/>");

        columns.Bound(o => o.Id).Width(50).Title("系统Id").Hidden();

        columns.Bound(o => o.Content).Title("推送内容");
        columns.Bound(o => o.IpAddress).Width(100).Title("添加IP").ReadOnly();
        columns.Bound(o => o.AddTime).Format("{0:yyyy-MM-dd HH:mm:ss}").Width(150).Title("添加时间").ReadOnly();
        columns.Bound(o => o.SortNum).Width(100).Title("排序号").Hidden();
        columns.Bound(o => o.State).Width(100).Title("状态").Hidden();
        columns.Command(comm =>
        {
            if (Html.HasUiPermission(this.ViewContext, "deletepushcontent"))
            {
                comm.Delete().ButtonType(GridButtonType.Text);
            }
        }).Width(100).Title("删除");
    })
    .ClientEvents(clientEvents => clientEvents.OnDataBinding("dataBinding").OnComplete("bindComplete"))
    .DataKeys(key => key.Add("Id"))//添加主键
    .DataBinding(dataBinding => dataBinding.Ajax().Select("ajaxpushcontentindex", "push")
        .Delete("deletepushcontent", "push"))
    .Pageable(page => page.PageSize(15))
    .Render();
}
<script type="text/javascript">
    function searchChange() {
        $("#PushContentGrid").data("tGrid").rebind();
    }

    function dataBinding(args) {
        var searchKeyStr = $("#searchPushContent").val();
        if (searchKeyStr == '历史内容') {
            searchKeyStr = '';
        }
        args.data = $.extend(args.data, { searchKey: searchKeyStr });
    }

    function mulDelete() {
        var delStr = $(':checked');
        if (delStr.length < 1) {
            alert('亲，您没有选择任何记录.');
            return;
        }
        jQuery.post('/push/muldeletepushcontent', delStr, function (res) {
            if (res.success) {
                setTimeout(function () {
                    $("#PushContentGrid").data("tGrid").rebind();
                    $("#checkAllRecords").attr("checked", false);
                }, 1000);
            } else {
                alert(res.message);
            }
        });
    }

    function bindComplete(e) {
        $("input:checkbox[name='selectRow']").attr("checked", false);
        var success = e.response.success;
        if (success != null && success != undefined) {
            alert(e.response.message);
            window.location.reload();
        }
    }

    $(function () {
        $("input:checkbox[name='selectRow']").attr("checked", false);
        $('#checkAllRecords').click(function checkAll() { $("#PushContentGrid tbody input:checkbox[name='selectRow']").attr("checked", this.checked); });
        $(":input[data-autocomplete]").each(function () {
            $(this).autocomplete({
                source: $(this).attr("data-autocomplete")
            });
        });
        $("#searchPushContent").focus(function () {
            if (this.value == "历史内容") {
                $("#searchPushContent").val("");
            }
        }).blur(function () {
            if (this.value == "") {
                $("#searchPushContent").val("历史内容");
            }
        });
    });
</script>
